package com.gaoyi.crm.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gaoyi.crm.R;
import com.gaoyi.crm.databinding.ViewDateSearchBinding;
import com.gaoyi.crm.util.DateUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.util.WindowUtil;

import java.util.Calendar;
import java.util.Date;

public class DateSearchView extends BaseView<ViewDateSearchBinding> {

    private String mFormat, mTime;
    private Calendar mStart, mEnd;
    private OnSearchClick mOnSearchClick;

    public void setOnSearchClick(OnSearchClick onSearchClick) {
        this.mOnSearchClick = onSearchClick;
    }

    public DateSearchView(@NonNull Context context) {
        super(context);
    }

    public DateSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DateSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DateSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initView(TypedArray ta) {
        mFormat = ta.getString(R.styleable.DateSearchView_date_search_format);
        mStart = Calendar.getInstance();
        mEnd = Calendar.getInstance();
        MyOnClickListener onClickListener = new MyOnClickListener();
        mBinding.tvStart.setOnClickListener(onClickListener);
        mBinding.tvEnd.setOnClickListener(onClickListener);
        mBinding.btnSearch.setOnClickListener(onClickListener);
    }

    private class MyOnClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.tv_start) {
                mTime = " 00:00:00";
                showDatePicker(mStart, mBinding.tvStart);
            } else if (id == R.id.tv_end) {
                mTime = " 23:59:59";
                showDatePicker(mEnd, mBinding.tvEnd);
            } else if (id == R.id.btn_search) {
                search();
            }
        }
    }

    private void search() {
        if (TextUtils.isEmpty(mBinding.tvStart.getText())) {
            ToastUtil.showToast(R.string.p_sel_start_date);
            return;
        }
        if (TextUtils.isEmpty(mBinding.tvEnd.getText())) {
            ToastUtil.showToast(R.string.p_sel_end_date);
            return;
        }
        if (mOnSearchClick != null) {
            mOnSearchClick.onSearchClick(mStart.getTime(), mEnd.getTime());
        }
    }

    private void showDatePicker(Calendar calendar, TextView tvShow) {
        WindowUtil.hideSoftInput(tvShow);
        DateUtil.showDatePicker(mContext, mFormat, calendar, (date) -> {
            String time = DateUtil.DateTimeToStr(date, mFormat);
            tvShow.setText(time);
            time += mTime;
            Date dateTime = DateUtil.StrToDateTime(time, "yyyy-MM-dd HH:mm:ss");
            calendar.setTime(dateTime == null ? date : dateTime);
        });
    }

    public Date getStartDate() {
        return mStart.getTime();
    }

    public Date getEndDate() {
        return mEnd.getTime();
    }

    public void setDate(Long start,Long end){
        if (start != null) {
            mStart.setTimeInMillis(start);
            mBinding.tvStart.setText(DateUtil.DateTimeToStr(mStart.getTime(), mFormat));
        }
        if (end != null) {
            mEnd.setTimeInMillis(end);
            mBinding.tvEnd.setText(DateUtil.DateTimeToStr(mEnd.getTime(), mFormat));
        }
    }

    public boolean isSelect() {
        return !TextUtils.isEmpty(mBinding.tvStart.getText()) && !TextUtils.isEmpty(mBinding.tvEnd.getText());
    }

    @Override
    protected ViewDateSearchBinding getViewBinding() {
        return ViewDateSearchBinding.inflate(LayoutInflater.from(mContext));
    }

    @Override
    protected int[] getAttrs() {
        return R.styleable.DateSearchView;
    }

    public interface OnSearchClick {
        void onSearchClick(Date startDate, Date endDate);
    }

}
